package com.itcast.hello.controller;

import com.itcast.hello.service.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.UUID;

@Controller
@CrossOrigin//跨域支持
public class SmsApiController {


    @Autowired
    private SendSms sendSms;

    @RequestMapping("/code/{phone}")
    public String code(@PathVariable("phone") String phone) {
        String code = UUID.randomUUID().toString().substring(0, 4);
        HashMap<String, Object> param = new HashMap<>();
        param.put("code", code);
        boolean send = sendSms.send(phone, "SMS_193238895", param);
        if (send) {
            return phone + ":" + code + "发送成功";
        }else{
            return phone + ":" + code + "发送失败";
        }
    }
}